/*
 ------------------------------------------------------------------------------
	sdlmedia - a test program for a media search library, including test UI.

	Copyright (C) 2011 Pierre Veber
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software Foundation,
	Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
------------------------------------------------------------------------------
*/

#include "common.h"
#include "thread.h"
#include "fastheap.h"
#include "sdlmedia.h"
#include "medialib.h"
#include "flatview.h"

#define THIS_FILTER	TRACE_DIR

View_t* pCurrentView = NULL;
SDL_Surface* pScreen = NULL;

AudioDevice_t	audio;
AudioDevice_t*	pAudio = &audio;

MediaFile_t* _gp_Current = NULL;

char* pszFileToPlay = NULL;

/*
 ------------------------------------------------------------------------------
	UI Specs
------------------------------------------------------------------------------
*/

/* size */
int _gi_AppWidth	= 320;
int _gi_AppHeight	= 480;

View_t* _view_show(View_t* pView)
{
	if (pCurrentView != NULL)
	{
		pCurrentView->fpHandleUIMessage(pCurrentView, UI_SHOW, NULL, 0);
	}
	pCurrentView = pView;
	pCurrentView->fpHandleUIMessage(pCurrentView, UI_SHOW, NULL, 1);
	return pCurrentView;
}

/*
-------------------------------------------------------------------------------
*/

int main(int argc, char* argv[])
{
	SDL_Event event;
	MediaLib_t* pLib = NULL;
	View_t* pFlatView;

	if (TTF_Init() == -1)
	{
		TRACE("Could not initialize SDL_TTF: %s\n", TTF_GetError());
		return 1;
	}	

	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		TRACE("Could not initialize SDL: %s\n", SDL_GetError());
		return 1;
	}
	
	if (IMG_Init(IMG_INIT_JPG|IMG_INIT_PNG) == -1)
	{
		TRACE("Could not initialize SDL_IMG: %s\n", IMG_GetError());
		return 1;
	}	

	pScreen = SDL_SetVideoMode(_gi_AppWidth, _gi_AppHeight, 0, SDL_HWPALETTE);
	if (pScreen == NULL)
	{
		TRACE("Couldn't set screen mode: %s\n", SDL_GetError());
		return 1;
	}

	SDL_WM_SetCaption("willy-player/PROOF/6", NULL);
	SDL_WM_SetIcon(IMG_Load("res/icon.png"), NULL);

	/* init audio */
	waveInit(pAudio);

	/* interpret command line */
	if (argc > 1)
	{
#ifdef WIN32
		PathUnquoteSpacesA(argv[1]);
#endif
		pLib = _media_lib_new(_path_find_dir(argv[1]));
		if (pLib != NULL)
		{
			pFlatView = CreateFlatView();
			_view_show(pFlatView);
			pszFileToPlay = _strdup(argv[1]);
			_media_lib_fill(pLib);
		}
	}

	//pLib = _media_lib_new("C:\\Users\\pveber\\Documents\\Storage\\Music");
	//pLib = _media_lib_new("C:\\Users\\pveber\\Documents\\Dev\\Current\\willy-player\\library");
	//pLib = _media_lib_new("C:\\Users\\pveber\\Documents\\Storage\\Music\\30 Seconds To Mars"); */
	/*if (pLib != NULL)
	{
		pFlatView = CreateFlatView();
		_view_show(pFlatView);
		_media_lib_fill(pLib);
	}*/
	
	while(SDL_WaitEvent(&event))
	{
		if ( (pCurrentView == NULL) || (pCurrentView->fpHandleEvent(pCurrentView, &event)) )
		{
			if (event.type == SDL_QUIT)
			{
				break;
			}
			else if (event.type == SDL_KEYDOWN)
			{
				switch (event.key.keysym.sym)
				{
					case SDLK_ESCAPE:
						/* unstack */
						break;

					case SDLK_SPACE:
						/* start playback */
						break;

					default:
						break;
				}
			}
		}
	}

	if (_gp_Current != NULL)
	{
		_gp_Current->pDecoder->fpMediaStop(_gp_Current->pDecodeData);
	}
	waveClose(pAudio);

	TTF_Quit();
	IMG_Quit();
	SDL_Quit();

	return 0;
}

#ifdef _MSC_VER
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "shlwapi.lib")
#pragma comment(lib, "../common/mad/libmad.lib")
#pragma comment(lib, "../common/sdl/SDL.lib")
#pragma comment(lib, "../common/sdl/SDL_Image.lib")
#pragma comment(lib, "../common/sdl/SDL_ttf.lib")
#pragma comment(lib, "../common/zlib/zlib.lib")

static char* _argv[] = { "sdlmedia.exe", "" };
int APIENTRY _tWinMain(
			HINSTANCE hInstance,
			HINSTANCE hPrevInstance,
			LPTSTR    lpCmdLine,
			int       nCmdShow
			)
{
	/* convert command line to UTF-8 */
	char* pszCmdLine = (char*)malloc(_tcslen(lpCmdLine)+1);
	WideCharToMultiByte(CP_UTF8, 0, lpCmdLine, -1 ,pszCmdLine, _tcslen(lpCmdLine)+1, NULL, NULL);
	_argv[1] = pszCmdLine;
	return main(2, _argv);
}

#endif
